#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from Utils.Timer import timer
from Utils.Logger import logger
from Src.ExcelParser import ExcelParser
from collections import defaultdict


class ScriptExcelParser(ExcelParser):
    """提供解析脚本表的特殊方法"""
    @timer(tracer=False)
    def load_config(self):
        """
        遍历"非直售皮肤.xlsx"的Setting表，读取设置
        :return: 一键多值字典settings_dict
        """
        ws = self.workbook['Setting']
        settings_dict = defaultdict(list)
        skin_type_sec_len, currency_type_sec_len, skin_label_sec_len = slice(0, 3), slice(5, 8), slice(10, 12)
        init_row = 1

        skin_type_title_list = [x.value for x in (ws[init_row][skin_type_sec_len])]
        currency_type_title_list = [x.value for x in (ws[init_row][currency_type_sec_len])]
        skin_label_title_list = [x.value for x in (ws[init_row][skin_label_sec_len])]

        for cur_row in range(init_row + 1, ws.max_row + 1):
            skin_type_val_list = list(filter(
                lambda x: x is not None, [x.value for x in (ws[cur_row][skin_type_sec_len])]))
            currency_type_val_list = list(filter(
                lambda x: x is not None, [x.value for x in (ws[cur_row][currency_type_sec_len])]))
            skin_label_val_list = list(filter(
                lambda x: x is not None, [x.value for x in (ws[cur_row][skin_label_sec_len])]))

            skin_type_dict = dict(map(lambda k, v: (k, v), skin_type_title_list, skin_type_val_list))
            currency_type_dict = dict(map(lambda k, v: (k, v), currency_type_title_list, currency_type_val_list))
            skin_label_dict = dict(map(lambda k, v: (k, v), skin_label_title_list, skin_label_val_list))

            if skin_type_dict:
                settings_dict['skin_type'].append(skin_type_dict)
            if currency_type_dict:
                settings_dict['currency_type'].append(currency_type_dict)
            if skin_label_dict:
                settings_dict['skin_label'].append(skin_label_dict)

        return settings_dict

    @timer(tracer=False)
    def insert_output(self, current_row, title_list, data_dict):
        """
        找到output工作表，将重构后的配置数据写入该工作表
        :param current_row: 工作表的当前行
        :param title_list: 配置项组成的list，写成工作表的抬头，因为顺序很重要，所以需要从配置表中读取
        :param data_dict: 配置项和配置值组成的dict
        :return: None
        """
        if 'Output' not in self.worksheet_names:
            self.workbook.create_sheet('Output')
        _ws = self.get_worksheet('Output')
        for index, title in enumerate(title_list):
            _ws.cell(row=current_row, column=index + 1).value = title
        for key, val in data_dict.items():
            try:
                _ws.cell(row=current_row + 1, column=title_list.index(key) + 1).value = val
            except ValueError:
                logger.debug('需要配置的项[{0}]在配置表[{1}]抬头[{2}]中不存在'.format(
                    key, data_dict['SheetName'], title_list))


if __name__ == '__main__':
    pass
